<?php

class Application_Model_KosaricaMapper
{
   
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Kosarica');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_Kosarica $kosarica, $insert = false)
    {
        $data = array(
            'active' => $kosarica->getActive(),
            'sifra_kosarice'   => $kosarica->getSifra_kosarice(),
            'stranka_emso' => $kosarica->getStranka_emso(),
            'total' => $kosarica->getTotal(),
        );

        try {
            if (!$insert) {
                $where = $this->getDbTable()->getAdapter()->quoteInto('sifra_kosarice = ?', $data['sifra_kosarice']);
                $this->getDbTable()->update($data, $where);
            } else {
                $this->getDbTable()->insert($data);
            }
        } catch (Exception $e) {
            echo "Error: ".$e->getMessage();
            die;
        }

    }

    public function find($id, Application_Model_Kosarica &$kosarica)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();

        $kosarica->setSifra_kosarice($row->sifra_kosarice)
            ->setActive($row->active)
            ->setStranka_emso($row->stranka_emso)
            ->setTotal($row->total);

    }

    public function fetchRow($where) {
        return $this->getDbTable()->fetchRow($where);
    }
    
    
    public function fetchAll($where) {
        return $this->getDbTable()->fetchAll($where);
    }
    
    
    
    public function delete($id)
    {
       $where = $this->getDbTable()->getAdapter()->quoteInto('sifra_kosarice = ?', $id);
       return $this->getDbTable()->delete($where);
    }
    
    
    public function getAllKosarice($where = null) {

        $kosaricaArray = $this->fetchAll($where);
        $entries = array();
        

        foreach ($kosaricaArray as $kosarica) {
            $entry = new Application_Model_Kosarica();
           // var_dump($kosarica);
           // die;
            $entry->setSifra_kosarice($kosarica['sifra_kosarice']);
                                                                
            $entry->setTotal($kosarica['total']);			
            $entry->setActive($kosarica['active']);			
            $entry->setStranka_emso($kosarica['stranka_emso']);

            $entries[$kosarica['sifra_kosarice']] = $entry;

        }
        return $entries;
    }
    
    
    public function update($sifra_kosarice, $polje, $vrednost) {
        $where = $this->getDbTable()->getAdapter()->quoteInto('sifra_kosarice = ?', $sifra_kosarice);
        $this->getDbTable()->update(array($polje => $vrednost), $where);
    }
    
    public function fetchAllAsArray($where) {
        $result = array();
        $kosaricaArray = $this->fetchAll($where);
        foreach ($kosaricaArray as $kosarica) {
            $kosaricaArr = array();

            $kosaricaArr['sifra_kosarice'] = $kosarica['sifra_kosarice'];
            $kosaricaArr['active'] = $kosarica['active'];
            $kosaricaArr['stranka_emso'] = $kosarica['stranka_emso'];
            $kosaricaArr['total'] = $kosarica['total'];


            $result[] = $kosaricaArr;
        }

        return $result;
    }
    
    
    
        public function getIzdelki($emso = null) {
            $izdelkiMapper = new Application_Model_IzdelkiMapper();
           $elementKosariceMapper = new Application_Model_ElementKosariceMapper();
            $where1=  $this->getDbTable()->getAdapter()->quoteInto('stranka_emso = ?', $emso);
           // $kosarica=$this->fetchRow($where1);

            $select = $this->getDbTable()->getAdapter()
                ->select()
                ->from('kosarica')
                ->where('stranka_emso = ?', $emso)
                ->where('active = 1');

            $stmt = $this->getDbTable()->getAdapter()->query($select);
            $kosarica = $stmt->fetchAll();
            $kosarica = $kosarica[0];

            if (!isset($kosarica['sifra_kosarice']) || !$kosarica['sifra_kosarice']) {
                return null;
            }

             $where = $elementKosariceMapper->getDbTable()->getAdapter()->quoteInto('kosarica_sifra_kosarice = ?', $kosarica['sifra_kosarice']);


        $elementKosariceArray = $elementKosariceMapper->fetchAll($where);
        $entries1 = array();

        if (!count($elementKosariceArray)) {
            return null;
        }



        foreach($elementKosariceArray as $element){
            $entries1[] = $element['produkt_idprodukt'];
            $kolicina[$element['produkt_idprodukt']] =$element['kolicina'] ;
            
        }

        $where2=$izdelkiMapper->getDbTable()->getAdapter()->quoteInto('idprodukt IN (?)', $entries1);

        $izdelkiArray = $izdelkiMapper->fetchAll($where2);

        $entries = array();
        
        foreach ($izdelkiArray as $izdelek) {
            
            $entry = new Application_Model_Izdelki();
            
            $entry->setIdProdukt($izdelek['idprodukt']);
            $entry->setIme($izdelek['ime']);
            $entry->setCena($izdelek['cena']);
            $entry->setZaloga($izdelek['zaloga']);
            $entry->setThumbNAil($izdelek['thumbnail']);
            $entry->setBaseImage($izdelek['base_image']);
            $entry->setAktiviran($izdelek['aktiviran']);
            
            $entries[$izdelek['idprodukt']] = array('izdelek'=>$entry,'kolicina'=>$kolicina[$entry->getIdProdukt()]);
            
        }
        return $entries;
    }


    public function getIzdelki2($kosarica = null) {
        $izdelkiMapper = new Application_Model_IzdelkiMapper();
        $elementKosariceMapper = new Application_Model_ElementKosariceMapper();

        $where = $elementKosariceMapper->getDbTable()->getAdapter()->quoteInto('kosarica_sifra_kosarice = ?', $kosarica);

        $elementKosariceArray = $elementKosariceMapper->fetchAll($where);
        $entries1 = array();

        if (!count($elementKosariceArray)) {
            return null;
        }

        foreach($elementKosariceArray as $element){
            $entries1[] = $element['produkt_idprodukt'];
            $kolicina[$element['produkt_idprodukt']] =$element['kolicina'] ;

        }

        $where2=$izdelkiMapper->getDbTable()->getAdapter()->quoteInto('idprodukt IN (?)', $entries1);

        $izdelkiArray = $izdelkiMapper->fetchAll($where2);

        $entries = array();

        foreach ($izdelkiArray as $izdelek) {

            $entry = new Application_Model_Izdelki();

            $entry->setIdProdukt($izdelek['idprodukt']);
            $entry->setIme($izdelek['ime']);
            $entry->setCena($izdelek['cena']);
            $entry->setZaloga($izdelek['zaloga']);
            $entry->setThumbNAil($izdelek['thumbnail']);
            $entry->setBaseImage($izdelek['base_image']);
            $entry->setAktiviran($izdelek['aktiviran']);

            $entries[$izdelek['idprodukt']] = array('izdelek'=>$entry,'kolicina'=>$kolicina[$entry->getIdProdukt()]);

        }
        return $entries;
    }

    public function getLastInsertId() {
        $row = $this->getDbTable()->getAdapter()->query('SELECT sifra_kosarice FROM kosarica ORDER BY sifra_kosarice DESC LIMIT 1')->fetchAll();
        return $row[0]['sifra_kosarice'];
    }

}

